package com.lagou.mr.wc;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;
/*采用全局排序的做法：
由于默认只有一个reduceTask,并且在reduceTask之前已经有Combiner机制,所以只需添加一个全局变量记录顺序即可
*/
public class DataSortReducer extends Reducer<IntWritable, NullWritable, Text, NullWritable> {

    Text keyout = new Text();
    //全局排序变量
    private static int countnum = 0;

    @Override
    protected void reduce(IntWritable key, Iterable<NullWritable> values, Context context) throws IOException, InterruptedException {
        for (NullWritable niv : values) {
            countnum++;
            String kk = countnum + "\t" + key.toString();
            keyout.set(kk);
            context.write(keyout, NullWritable.get());
        }

    }
}
